package com.socket.socket;

import java.io.OutputStream;
import java.net.Socket;

public class TcpClientSimulator {
    public static void main(String[] args) {
        String serverIp = "118.89.53.22";
        int serverPort = 7888;

        try {
            Socket socket = new Socket(serverIp, serverPort);
            OutputStream outputStream = socket.getOutputStream();

            // 模拟发送 5 条数据
            for (int i = 1; i <= 5; i++) {
                String message = "TCP TEST client send: " + i + "\n";
                outputStream.write(message.getBytes());
                outputStream.flush();
                System.out.println("发送数据：" + message);
                Thread.sleep(1000); // 模拟间隔发送
            }

            // 保持连接，模拟单片机的行为
            Thread.sleep(10000);
            socket = new Socket(serverIp, serverPort);
            outputStream = socket.getOutputStream();

            // 模拟发送 5 条数据
            for (int i = 1; i <= 5; i++) {
                String message = "TCP TEST client send: " + i + "\n";
                outputStream.write(message.getBytes());
                outputStream.flush();
                System.out.println("发送数据：" + message);
                Thread.sleep(1000); // 模拟间隔发送
            }
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
